package com.version1_3.dao.implement;

import com.SQLGroup.SQLAccess;
import com.alibaba.fastjson.JSONObject;
import com.util.httpRequest.HttpRequest;
import com.version1_3.dao.ShareDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by kuibu on 16/1/26.
 */
@Repository
public class ShareDaoImpl implements ShareDao {
    private static final Logger LOGGER = Logger.getLogger(ShareDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public boolean doRecordPageReaded(int pageId) {

        int row = jdbcTemplate.update(SQLAccess.UpdatePageWebReadedNumberVersion1_3, new Object[]{pageId});
        return (row == 1) ? true : false;
    }

    /**
     * 查询用户邀请码
     *
     * @param userId
     * @return
     */
    @Override
    public String getUserInviteCode(int userId) {
        return jdbcTemplate.queryForObject(SQLAccess.SelectUserInviteCodeVersion1_3, new Object[]{userId}, java.lang.String.class);
    }

    /**
     * 查询用户邀请好友人数
     *
     * @param inviteCode
     * @return
     */
    @Override
    public int getUserInvitedFriends(String inviteCode) {
        return jdbcTemplate.queryForObject(SQLAccess.SelectUserInvitedFriendsNumberVersion1_3, new Object[]{inviteCode}, java.lang.Integer.class);
    }

    /**
     * 用户输入邀请码
     *
     * @param inviteCode
     * @return
     */
    @Override
    public boolean submitInvite(int userId, String inviteCode) {
        int row = jdbcTemplate.update(SQLAccess.UpdateUserInviteCodeVersion1_3, new Object[]{inviteCode, userId});
        return (row == 1) ? true : false;
    }

    /**
     * 用户是否已经输入了邀请码
     *
     * @param userId
     * @return
     */
    @Override
    public boolean getUserHasInputInviteCode(int userId) {
        Map<String, Object> inviteCodeMap = jdbcTemplate.queryForMap(SQLAccess.SelectUserInputInviteCodeVersion1_3, new Object[]{userId});

        // 如果用户是第三方登陆的,不允许输入邀请码
        if (((String) inviteCodeMap.get("userTel")).isEmpty()) {
            return true;
        }

        return ("null".equals(inviteCodeMap.get("inputInviteCode"))) ? false : true;
    }


    /**
     * TODO: 获取第三方用户信息
     *
     * @param platform
     * @param openid
     * @param token
     * @return
     */
    @Override
    public Map<String, Object> getThirdPartUserInfoMap(String platform, String os, String openid, String token) {
        String requestURL = "";
        Map<String, Object> thirdPartUserInfoMap = new HashMap<>();
        String userName = "";
        String userPhoto = "";
        String userSex = "";
        String responseStr = "";
        JSONObject jsonObject = null;

        switch (platform) {

            // 微信平台 - 开发者认证正在进行
            case "wechat":
                requestURL = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openid;
                responseStr = HttpRequest.sendGet(requestURL);
                jsonObject = JSONObject.parseObject(responseStr);
                userName = (String) jsonObject.get("nickname");
                userPhoto = (String) jsonObject.get("headimgurl");
                userSex = String.valueOf((int) jsonObject.get("sex"));

                if ("1".equals(userSex)) {
                    userSex = "男";
                } else {
                    userSex = "女";
                }
                break;

            // 新浪微博
            case "sina":
                requestURL = "https://api.weibo.com/2/users/show.json?access_token=" + token + "&uid=" + openid;
                responseStr = HttpRequest.sendGet(requestURL);
                jsonObject = JSONObject.parseObject(responseStr);
                userName = (String) jsonObject.get("screen_name");
                userPhoto = (String) jsonObject.get("avatar_large");
                userSex = (String) jsonObject.get("gender");

                if ("m".equals(userSex)) {
                    userSex = "男";
                } else {
                    userSex = "女";
                }
                break;

            // QQ
            case "qq":

                if ("android".equals(os)) {
                    requestURL = "https://graph.qq.com/user/get_user_info?access_token=" + token + "&oauth_consumer_key=1104847454&openid=" + openid + "&format=json";
                } else {
                    requestURL = "https://graph.qq.com/user/get_user_info?access_token=" + token + "&oauth_consumer_key=1104745162&openid=" + openid + "&format=json";
                }

                responseStr = HttpRequest.sendGet(requestURL);
                jsonObject = JSONObject.parseObject(responseStr);
                userName = (String) jsonObject.get("nickname");
                userPhoto = (String) jsonObject.get("figureurl_qq_1");
                userSex = (String) jsonObject.get("gender");

                break;
        }

        thirdPartUserInfoMap.put("userName", userName);
        thirdPartUserInfoMap.put("userPhoto", userPhoto);
        thirdPartUserInfoMap.put("userSex", userSex);

        return thirdPartUserInfoMap;
    }
}
